/*   1:    */ package com.atomikos.icatch.system;
/*   2:    */ 
/*   3:    */ import com.atomikos.datasource.RecoverableResource;
/*   4:    */ import com.atomikos.diagnostics.CascadedConsole;
/*   5:    */ import com.atomikos.diagnostics.Console;
/*   6:    */ import com.atomikos.icatch.CompositeTransactionManager;
/*   7:    */ import com.atomikos.icatch.ExportingTransactionManager;
/*   8:    */ import com.atomikos.icatch.ImportingTransactionManager;
/*   9:    */ import com.atomikos.icatch.RecoveryService;
/*  10:    */ import com.atomikos.icatch.TSListener;
/*  11:    */ import com.atomikos.icatch.TransactionService;
/*  12:    */ import com.atomikos.icatch.admin.LogAdministrator;
/*  13:    */ import com.atomikos.icatch.admin.LogControl;
/*  14:    */ import com.atomikos.util.ClassLoadingHelper;
/*  15:    */ import com.atomikos.util.ExceptionHelper;
/*  16:    */ import java.io.IOException;
/*  17:    */ import java.io.InputStream;
/*  18:    */ import java.net.URL;
/*  19:    */ import java.util.ArrayList;
/*  20:    */ import java.util.Enumeration;
/*  21:    */ import java.util.Hashtable;
/*  22:    */ import java.util.Iterator;
/*  23:    */ import java.util.List;
/*  24:    */ import java.util.Properties;
/*  25:    */ import java.util.Vector;
/*  26:    */ 
/*  27:    */ public final class Configuration
/*  28:    */ {
/*  29: 66 */   private static CompositeTransactionManager ctxmgr_ = null;
/*  30: 69 */   private static ImportingTransactionManager imptxmgr_ = null;
/*  31: 70 */   private static ExportingTransactionManager exptxmgr_ = null;
/*  32: 72 */   private static Hashtable resources_ = new Hashtable();
/*  33:    */   private static Console console_;
/*  34: 79 */   private static Vector resourceList_ = new Vector();
/*  35: 83 */   private static Vector logAdministrators_ = new Vector();
/*  36:    */   private static LogControl logControl_;
/*  37:    */   private static RecoveryService recoveryService_;
/*  38:    */   private static TransactionService service_;
/*  39: 95 */   private static Vector tsListenersList_ = new Vector();
/*  40: 97 */   private static List shutdownHooks_ = new ArrayList();
/*  41:    */   
/*  42:    */   public static String getVersion()
/*  43:    */   {
/*  44:100 */     String ret = "UNKNOWN";
/*  45:101 */     Properties props = new Properties();
/*  46:102 */     URL propertyFileUrl = ClassLoadingHelper.loadResourceFromClasspath(Configuration.class, "module.properties");
/*  47:103 */     if (propertyFileUrl != null)
/*  48:    */     {
/*  49:    */       try
/*  50:    */       {
/*  51:106 */         InputStream in = propertyFileUrl.openStream();
/*  52:107 */         props.load(in);
/*  53:108 */         in.close();
/*  54:    */       }
/*  55:    */       catch (IOException e)
/*  56:    */       {
/*  57:110 */         logWarning("Could not determine runtime version", e);
/*  58:    */       }
/*  59:112 */       String version = props.getProperty("product.version");
/*  60:113 */       if (version != null) {
/*  61:113 */         ret = version;
/*  62:    */       }
/*  63:    */     }
/*  64:115 */     return ret;
/*  65:    */   }
/*  66:    */   
/*  67:    */   private static void purgeResources()
/*  68:    */   {
/*  69:120 */     Enumeration enumm = getResources();
/*  70:121 */     while (enumm.hasMoreElements())
/*  71:    */     {
/*  72:122 */       RecoverableResource res = (RecoverableResource)enumm.nextElement();
/*  73:123 */       if (res.isClosed()) {
/*  74:124 */         removeResource(res.getName());
/*  75:    */       }
/*  76:    */     }
/*  77:    */   }
/*  78:    */   
/*  79:    */   public static synchronized void installTransactionService(TransactionService service)
/*  80:    */   {
/*  81:146 */     service_ = service;
/*  82:147 */     Iterator it = tsListenersList_.iterator();
/*  83:148 */     while ((it.hasNext()) && (service != null))
/*  84:    */     {
/*  85:149 */       TSListener l = (TSListener)it.next();
/*  86:150 */       service_.addTSListener(l);
/*  87:    */     }
/*  88:    */   }
/*  89:    */   
/*  90:    */   public static synchronized void addShutdownHook(Thread hook)
/*  91:    */   {
/*  92:164 */     if (shutdownHooks_.contains(hook)) {
/*  93:164 */       return;
/*  94:    */     }
/*  95:166 */     shutdownHooks_.add(hook);
/*  96:    */     try
/*  97:    */     {
/*  98:168 */       Runtime.getRuntime().addShutdownHook(hook);
/*  99:    */     }
/* 100:    */     catch (IllegalStateException alreadyShuttingDownVm) {}
/* 101:    */   }
/* 102:    */   
/* 103:    */   public static synchronized void removeShutdownHooks()
/* 104:    */   {
/* 105:183 */     Iterator it = shutdownHooks_.iterator();
/* 106:    */     
/* 107:    */ 
/* 108:    */ 
/* 109:187 */     boolean vmShutdown = false;
/* 110:188 */     while (it.hasNext())
/* 111:    */     {
/* 112:189 */       Thread t = (Thread)it.next();
/* 113:190 */       if (t.equals(Thread.currentThread())) {
/* 114:190 */         vmShutdown = true;
/* 115:    */       }
/* 116:    */     }
/* 117:193 */     it = shutdownHooks_.iterator();
/* 118:194 */     while ((!vmShutdown) && (it.hasNext()))
/* 119:    */     {
/* 120:195 */       Thread hook = (Thread)it.next();
/* 121:196 */       it.remove();
/* 122:    */       try
/* 123:    */       {
/* 124:198 */         Runtime.getRuntime().removeShutdownHook(hook);
/* 125:    */       }
/* 126:    */       catch (IllegalStateException alreadyShuttingDownVm) {}
/* 127:    */     }
/* 128:    */   }
/* 129:    */   
/* 130:    */   public static TransactionService getTransactionService()
/* 131:    */   {
/* 132:215 */     return service_;
/* 133:    */   }
/* 134:    */   
/* 135:    */   public static synchronized void addTSListener(TSListener l)
/* 136:    */   {
/* 137:227 */     if (service_ != null) {
/* 138:228 */       service_.addTSListener(l);
/* 139:    */     }
/* 140:230 */     tsListenersList_.add(l);
/* 141:    */   }
/* 142:    */   
/* 143:    */   public static synchronized void removeTSListener(TSListener l)
/* 144:    */   {
/* 145:241 */     if (service_ != null) {
/* 146:242 */       service_.removeTSListener(l);
/* 147:    */     }
/* 148:244 */     tsListenersList_.remove(l);
/* 149:    */   }
/* 150:    */   
/* 151:    */   public static synchronized void installCompositeTransactionManager(CompositeTransactionManager compositeTransactionManager)
/* 152:    */   {
/* 153:258 */     ctxmgr_ = compositeTransactionManager;
/* 154:    */   }
/* 155:    */   
/* 156:    */   public static synchronized void installRecoveryService(RecoveryService service)
/* 157:    */   {
/* 158:271 */     recoveryService_ = service;
/* 159:272 */     if (service != null)
/* 160:    */     {
/* 161:274 */       Enumeration resources = getResources();
/* 162:275 */       while (resources.hasMoreElements())
/* 163:    */       {
/* 164:276 */         RecoverableResource next = (RecoverableResource)resources.nextElement();
/* 165:    */         
/* 166:278 */         next.setRecoveryService(service);
/* 167:    */       }
/* 168:    */     }
/* 169:    */   }
/* 170:    */   
/* 171:    */   public static synchronized void installLogControl(LogControl control)
/* 172:    */   {
/* 173:294 */     logControl_ = control;
/* 174:295 */     if (logControl_ != null)
/* 175:    */     {
/* 176:296 */       Enumeration enumm = getLogAdministrators();
/* 177:297 */       while (enumm.hasMoreElements())
/* 178:    */       {
/* 179:298 */         LogAdministrator admin = (LogAdministrator)enumm.nextElement();
/* 180:299 */         admin.registerLogControl(control);
/* 181:    */       }
/* 182:    */     }
/* 183:    */   }
/* 184:    */   
/* 185:    */   public static synchronized void installImportingTransactionManager(ImportingTransactionManager importingTransactionManager)
/* 186:    */   {
/* 187:315 */     imptxmgr_ = importingTransactionManager;
/* 188:    */   }
/* 189:    */   
/* 190:    */   public static synchronized void installExportingTransactionManager(ExportingTransactionManager exportingTransactionManager)
/* 191:    */   {
/* 192:329 */     exptxmgr_ = exportingTransactionManager;
/* 193:    */   }
/* 194:    */   
/* 195:    */   public static CompositeTransactionManager getCompositeTransactionManager()
/* 196:    */   {
/* 197:340 */     return ctxmgr_;
/* 198:    */   }
/* 199:    */   
/* 200:    */   public static ImportingTransactionManager getImportingTransactionManager()
/* 201:    */   {
/* 202:351 */     return imptxmgr_;
/* 203:    */   }
/* 204:    */   
/* 205:    */   public static ExportingTransactionManager getExportingTransactionManager()
/* 206:    */   {
/* 207:362 */     return exptxmgr_;
/* 208:    */   }
/* 209:    */   
/* 210:    */   public static synchronized void addResource(RecoverableResource resource)
/* 211:    */     throws IllegalStateException
/* 212:    */   {
/* 213:387 */     purgeResources();
/* 214:388 */     int numResources = resources_.size();
/* 215:389 */     if (numResources > 100)
/* 216:    */     {
/* 217:390 */       logWarning(numResources + " RESOURCES IN CONFIGURATION -- " + "SOME XARESOURCE IMPLEMENTATIONS MAY NOT CORRECTLY IMPLEMENT isSameRM()!");
/* 218:    */       
/* 219:    */ 
/* 220:393 */       logWarning("TO SAVE MEMORY, USE EXPLICIT RESOURCE REGISTRATION MODE.");
/* 221:    */     }
/* 222:396 */     if (resources_.containsKey(resource.getName())) {
/* 223:397 */       throw new IllegalStateException("Attempt to register second resource with name " + resource.getName());
/* 224:    */     }
/* 225:400 */     logDebug("Configuration: adding resource " + resource.getName());
/* 226:    */     
/* 227:402 */     resources_.put(resource.getName(), resource);
/* 228:403 */     resourceList_.add(resource);
/* 229:404 */     resource.setRecoveryService(recoveryService_);
/* 230:    */     
/* 231:406 */     logDebug("Configuration: added resource " + resource.getName());
/* 232:    */   }
/* 233:    */   
/* 234:    */   public static synchronized void addLogAdministrator(LogAdministrator admin)
/* 235:    */   {
/* 236:416 */     if (logAdministrators_.contains(admin)) {
/* 237:416 */       return;
/* 238:    */     }
/* 239:418 */     logAdministrators_.add(admin);
/* 240:419 */     if (logControl_ != null) {
/* 241:420 */       admin.registerLogControl(logControl_);
/* 242:    */     }
/* 243:    */   }
/* 244:    */   
/* 245:    */   public static void removeLogAdministrator(LogAdministrator admin)
/* 246:    */   {
/* 247:431 */     logAdministrators_.remove(admin);
/* 248:432 */     if (logControl_ != null) {
/* 249:433 */       admin.deregisterLogControl(logControl_);
/* 250:    */     }
/* 251:    */   }
/* 252:    */   
/* 253:    */   public static Enumeration getLogAdministrators()
/* 254:    */   {
/* 255:443 */     Vector v = (Vector)logAdministrators_.clone();
/* 256:444 */     return v.elements();
/* 257:    */   }
/* 258:    */   
/* 259:    */   public static RecoverableResource removeResource(String name)
/* 260:    */   {
/* 261:457 */     RecoverableResource ret = null;
/* 262:458 */     if (name != null)
/* 263:    */     {
/* 264:459 */       ret = (RecoverableResource)resources_.remove(name);
/* 265:460 */       if (ret != null) {
/* 266:460 */         resourceList_.remove(ret);
/* 267:    */       }
/* 268:    */     }
/* 269:463 */     logDebug("Configuration: removed resource " + name);
/* 270:464 */     return ret;
/* 271:    */   }
/* 272:    */   
/* 273:    */   public static RecoverableResource getResource(String name)
/* 274:    */   {
/* 275:477 */     RecoverableResource res = null;
/* 276:478 */     if (name != null) {
/* 277:478 */       res = (RecoverableResource)resources_.get(name);
/* 278:    */     }
/* 279:479 */     return res;
/* 280:    */   }
/* 281:    */   
/* 282:    */   public static Enumeration getResources()
/* 283:    */   {
/* 284:492 */     Vector ret = (Vector)resourceList_.clone();
/* 285:493 */     return ret.elements();
/* 286:    */   }
/* 287:    */   
/* 288:    */   public static synchronized void addConsole(Console console)
/* 289:    */   {
/* 290:506 */     if (console_ == null)
/* 291:    */     {
/* 292:507 */       console_ = console;
/* 293:    */     }
/* 294:    */     else
/* 295:    */     {
/* 296:509 */       CascadedConsole cc = new CascadedConsole(console_, console);
/* 297:510 */       console_ = cc;
/* 298:    */     }
/* 299:    */   }
/* 300:    */   
/* 301:    */   public static Console getConsole()
/* 302:    */   {
/* 303:524 */     return console_;
/* 304:    */   }
/* 305:    */   
/* 306:    */   public static synchronized void removeConsoles()
/* 307:    */   {
/* 308:534 */     if (console_ != null)
/* 309:    */     {
/* 310:    */       try
/* 311:    */       {
/* 312:536 */         console_.close();
/* 313:    */       }
/* 314:    */       catch (Exception ignore) {}
/* 315:540 */       console_ = null;
/* 316:    */     }
/* 317:    */   }
/* 318:    */   
/* 319:    */   public static void logWarning(String msg)
/* 320:    */   {
/* 321:552 */     log(msg, 1);
/* 322:    */   }
/* 323:    */   
/* 324:    */   public static void logWarning(String msg, Throwable error)
/* 325:    */   {
/* 326:563 */     log(msg, error, 1);
/* 327:    */   }
/* 328:    */   
/* 329:    */   public static void logInfo(String msg)
/* 330:    */   {
/* 331:573 */     log(msg, 2);
/* 332:    */   }
/* 333:    */   
/* 334:    */   public static void logInfo(String msg, Throwable error)
/* 335:    */   {
/* 336:584 */     log(msg, error, 2);
/* 337:    */   }
/* 338:    */   
/* 339:    */   public static void logDebug(String msg)
/* 340:    */   {
/* 341:595 */     log(msg, 3);
/* 342:    */   }
/* 343:    */   
/* 344:    */   public static void logDebug(String msg, Throwable error)
/* 345:    */   {
/* 346:607 */     log(msg, error, 3);
/* 347:    */   }
/* 348:    */   
/* 349:    */   private static void log(String msg, int level)
/* 350:    */   {
/* 351:    */     try
/* 352:    */     {
/* 353:613 */       Console console = getConsole();
/* 354:614 */       if (console != null) {
/* 355:615 */         console.println(msg, level);
/* 356:    */       }
/* 357:    */     }
/* 358:    */     catch (Exception ignore) {}
/* 359:    */   }
/* 360:    */   
/* 361:    */   private static void log(String msg, Throwable e, int level)
/* 362:    */   {
/* 363:623 */     if (e != null)
/* 364:    */     {
/* 365:624 */       String stackTrace = ExceptionHelper.convertStackTrace(e);
/* 366:625 */       log(msg + "\n" + stackTrace, level);
/* 367:    */     }
/* 368:    */     else
/* 369:    */     {
/* 370:627 */       log(msg, level);
/* 371:    */     }
/* 372:    */   }
/* 373:    */   
/* 374:    */   public static boolean isDebugLoggingEnabled()
/* 375:    */   {
/* 376:641 */     boolean ret = false;
/* 377:642 */     Console console = getConsole();
/* 378:643 */     if (console != null) {
/* 379:644 */       ret = console.getLevel() == 3;
/* 380:    */     }
/* 381:646 */     return ret;
/* 382:    */   }
/* 383:    */   
/* 384:    */   public static boolean isInfoLoggingEnabled()
/* 385:    */   {
/* 386:657 */     boolean info = false;
/* 387:658 */     boolean debug = isDebugLoggingEnabled();
/* 388:659 */     Console console = getConsole();
/* 389:660 */     if (console != null) {
/* 390:661 */       info = console.getLevel() == 2;
/* 391:    */     }
/* 392:663 */     return (debug) || (info);
/* 393:    */   }
/* 394:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.system.Configuration
 * JD-Core Version:    0.7.0.1
 */